package ndquangr.intern.ringtonemaker;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ndquangr.intern.ringtonemaker.listitems.BrowseCateAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListView;

public class BrowseActivity extends Activity {
	private ListView lsv_browse_categories;
	private BrowseCateAdapter adtCategories;	
	private List<String> listItems;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browse_window);
        
        lsv_browse_categories = (ListView)findViewById(R.id.lsv_browse_musicCategories);
        
        Bundle extras = getIntent().getExtras();
		if (extras != null) {	
			// Get specific songs base on tag
			String tag = extras.getString("tag");
			if (tag.equalsIgnoreCase(Constants.MUSIC_CATEGORIES[1])) {
				// View by artists
				getAllArtists();
			} else if (tag.equalsIgnoreCase(Constants.MUSIC_CATEGORIES[2])) {
				// View by albums
				getAllAlbums();
			} else if (tag.equalsIgnoreCase(Constants.MUSIC_CATEGORIES[3])) {
				// View by genres
				getAllGenres();
			} else if (tag.equalsIgnoreCase(Constants.MUSIC_CATEGORIES[4])) {
				// View by folder
				getAllFolders();
			}	
			
			if (listItems != null) {				
				adtCategories = new BrowseCateAdapter(this, R.layout.browse_cate_item, new ArrayList<String>(listItems));
			}
			
		} else {        
			listItems = new ArrayList<String>();
			listItems.addAll(Arrays.asList(Constants.MUSIC_CATEGORIES)) ;
			listItems.add(0, "");
			adtCategories = new BrowseCateAdapter(this, R.layout.browse_cate_item, new ArrayList<String>(listItems));
		}
		
		if (adtCategories != null) {
			lsv_browse_categories.setAdapter(adtCategories);
		}
        
    }
    
	private void getAllFolders() {
		//Some audio may be explicitly marked as not being music
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
	
		String[] projection = {
				"DISTINCT " + MediaStore.Audio.Media.DATA
		};
	
		Cursor cursor = this.managedQuery(
		        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
		        projection,
		        selection,
		        null,
		        MediaStore.Audio.Media.DATA);
	
		listItems = new ArrayList<String>();
		String item, new_item;
		String splited_item [];
		int splited_part;
		while(cursor.moveToNext()){		 
			item = cursor.getString(0);
			splited_item = item.split("/");
			splited_part = splited_item.length;
			if (splited_part > 2) {
				new_item = "";
				for (int i = 2; i < splited_part - 1; i++) {
					new_item = new_item + splited_item[i];
					if (i < splited_part - 2) {
						new_item = new_item + "/";
					}
				}		
				if (!new_item.equals("") && !listItems.contains(new_item)) {
					listItems.add(new_item);
				}
			}			
		}
		listItems.add(0, Constants.MUSIC_CATEGORIES[4]);
	}
	
	private void getAllGenres() {
		
		String[] projection = {
				"DISTINCT " + MediaStore.Audio.Genres.NAME
		};
	
		Cursor cursor = this.managedQuery(
				MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI,
		        projection,
		        null,
		        null,
		        MediaStore.Audio.Genres.NAME);
	
		listItems = new ArrayList<String>();
		
		while(cursor.moveToNext()){		    
			listItems.add(cursor.getString(0));
		}
		
		listItems.add(0, Constants.MUSIC_CATEGORIES[3]);
	}
	
	private void getAllAlbums() {
		//Some audio may be explicitly marked as not being music
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
	
		String[] projection = {
				"DISTINCT " + MediaStore.Audio.Media.ALBUM
		};
	
		Cursor cursor = this.managedQuery(
		        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
		        projection,
		        selection,
		        null,
		        MediaStore.Audio.Media.ALBUM);
	
		listItems = new ArrayList<String>();
		
		while(cursor.moveToNext()){		    
			listItems.add(cursor.getString(0));
		}
		listItems.add(0, Constants.MUSIC_CATEGORIES[2]);
	}
	
	private void getAllArtists() {
		//Some audio may be explicitly marked as not being music
		String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
	
		String[] projection = {
				"DISTINCT " + MediaStore.Audio.Media.ARTIST
		};
	
		Cursor cursor = this.managedQuery(
		        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
		        projection,
		        selection,
		        null,
		        MediaStore.Audio.Media.ARTIST);
	
		listItems = new ArrayList<String>();
		
		while(cursor.moveToNext()){		    
			listItems.add(cursor.getString(0));
		}
		
		listItems.add(0, Constants.MUSIC_CATEGORIES[1]);
	}
}
